package com.ruoyi.system.service.impl;

import java.util.List;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.domain.Orders;
import com.ruoyi.system.annotation.UserContext;
import com.ruoyi.system.domain.vo.UserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.common.core.domain.TbCustomerOrderDetail;
import com.ruoyi.system.mapper.TbCustomerOrderMapper;
import com.ruoyi.system.domain.TbCustomerOrder;
import com.ruoyi.system.service.ITbCustomerOrderService;
import org.springframework.web.client.RestTemplate;

/**
 * 订单管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-12
 */
@Service
public class TbCustomerOrderServiceImpl implements ITbCustomerOrderService 
{
    @Autowired
    private TbCustomerOrderMapper tbCustomerOrderMapper;


    @Autowired
    private RestTemplate restTemplate;



    /**
     * 新增未支付订单信息
     *
     * @return
     */
    public String add(Orders orders) {

        System.out.println(orders.getOrderCallnum() + "------");

        UserData userData = UserContext.getUserData();

        orders.setMerchantId((long) userData.getMerchantId());
//
        orders.setStoreId((long) userData.getStoreId());

        orders.setOrderNo(IdUtil.getSnowflakeNextIdStr()); //订单号
        orders.setOrderStatusId(5L);  //状态id
        orders.setCreateTime(DateUtil.now());  //创建时间

        System.out.println(orders);

        int num = tbCustomerOrderMapper.insert(orders);

        String url = null;
        if (num != 0) {
           //插入订单详情
            tbCustomerOrderMapper.insertOrderDetails(orders.getOrderNo(),orders.getTbCustomerOrderDetail());

            try {
                url = "http://127.0.0.1:8888/alipay/pay?orderNo=" + orders.getOrderNo();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

        return url;
    }


    //查询订单详情
    @Override
    public List<TbCustomerOrderDetail> selectTbCustomerOrderByOrderNumberDetail(String orderNumber) {

        return tbCustomerOrderMapper.selectTbCustomerOrderByOrderNumberDetail(orderNumber);
    }

    /**
     * 查询订单管理
     * 
     * @param orderNumber 订单管理主键
     * @return 订单管理
     */
    @Override
    public TbCustomerOrder selectTbCustomerOrderByOrderNumber(Long orderNumber)
    {

        try {
            UserData userData = UserContext.getUserData();

            int merchantId = userData.getMerchantId();

            int storeId = userData.getStoreId();

            String tableName = "tb_customer_order" +  userData.getTableName();

            return tbCustomerOrderMapper.selectTbCustomerOrderByOrderNumber(tableName,merchantId,storeId,orderNumber);
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }


    }

    /**
     * 商家查询订单管理列表
     * 
     * @param tbCustomerOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<TbCustomerOrder> selectTbCustomerOrderList(TbCustomerOrder tbCustomerOrder)
    {

        try {

            UserData userData = UserContext.getUserData();

            int merchantId = userData.getMerchantId();

            int storeId = userData.getStoreId();

            tbCustomerOrder.setMerchantId((long) merchantId);

            tbCustomerOrder.setStoreId((long) storeId);

            return tbCustomerOrderMapper.selectTbCustomerOrderList(tbCustomerOrder);
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }

    }

    /**
     * 新增订单管理
     * 
     * @param tbCustomerOrder 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbCustomerOrder(TbCustomerOrder tbCustomerOrder)
    {
        int rows = tbCustomerOrderMapper.insertTbCustomerOrder(tbCustomerOrder);
        insertTbCustomerOrderDetail(tbCustomerOrder);
        return rows;
    }

    /**
     * 修改订单管理
     * 
     * @param tbCustomerOrder 订单管理
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbCustomerOrder(TbCustomerOrder tbCustomerOrder)
    {
        tbCustomerOrderMapper.deleteTbCustomerOrderDetailByOrderNumber(tbCustomerOrder.getOrderNumber());
        insertTbCustomerOrderDetail(tbCustomerOrder);
        return tbCustomerOrderMapper.updateTbCustomerOrder(tbCustomerOrder);
    }

    /**
     * 批量删除订单管理
     * 
     * @param orderNumbers 需要删除的订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbCustomerOrderByOrderNumbers(Long[] orderNumbers)
    {
        tbCustomerOrderMapper.deleteTbCustomerOrderDetailByOrderNumbers(orderNumbers);
        return tbCustomerOrderMapper.deleteTbCustomerOrderByOrderNumbers(orderNumbers);
    }

    /**
     * 删除订单管理信息
     * 
     * @param orderNumber 订单管理主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbCustomerOrderByOrderNumber(String orderNumber)
    {
        tbCustomerOrderMapper.deleteTbCustomerOrderDetailByOrderNumber(orderNumber);
        return tbCustomerOrderMapper.deleteTbCustomerOrderByOrderNumber(orderNumber);
    }



    /**
     * 新增商家-顾客：订单详情
买了什么东西 、  数量  、  价格信息
     * 
     * @param tbCustomerOrder 订单管理对象
     */
    public void insertTbCustomerOrderDetail(TbCustomerOrder tbCustomerOrder)
    {
        List<TbCustomerOrderDetail> tbCustomerOrderDetailList = tbCustomerOrder.getTbCustomerOrderDetailList();
        String orderNumber = tbCustomerOrder.getOrderNumber();
        if (StringUtils.isNotNull(tbCustomerOrderDetailList))
        {
            List<TbCustomerOrderDetail> list = new ArrayList<TbCustomerOrderDetail>();
            for (TbCustomerOrderDetail tbCustomerOrderDetail : tbCustomerOrderDetailList)
            {
                tbCustomerOrderDetail.setOrderNumber(orderNumber);
                list.add(tbCustomerOrderDetail);
            }
            if (list.size() > 0)
            {
                tbCustomerOrderMapper.batchTbCustomerOrderDetail(list);
            }
        }
    }
}
